package com.primaryedu.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.HomenewsBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thomas_yang on 2018/10/11.
 */

public class HomenewsAdapter  extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
//    LoadImage loadImage;
    private List<HomenewsBean.DataBean.ListBean> newsLists = new ArrayList<>();

    public HomenewsAdapter(Context context, List<HomenewsBean.DataBean.ListBean> newsLists) {
        this.context = context;
        this.newsLists = newsLists;
//        loadImage = LoadImage.getInstance();
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return newsLists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomenewsAdapter.ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new HomenewsAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.item_exercise, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.title = (TextView) convertView.findViewById(R.id.text_title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.text_content);
            viewHolder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (HomenewsAdapter.ViewHolder) convertView.getTag();
        }

        HomenewsBean.DataBean.ListBean bean = newsLists.get(position);
        Log.e("yy", "getPicurl: =====" + bean.getPicurl());
//        viewHolder.image.setTag(bean.getPicurl());
//        loadImage.addTask(bean.getPicurl(), viewHolder.image);
//        loadImage.doTask();
        Glide.with(convertView.getContext())
                .load(bean.getPicurl())
                .placeholder(R.drawable.new_bg_default)
                .error(R.drawable.new_bg_default)
                .centerCrop()
                .crossFade()
                .into(viewHolder.image);


        viewHolder.title.setText(bean.getTitle());
        viewHolder.content.setText(bean.getSecondTitle());
        viewHolder.text_date.setText(bean.getReleaseTime());

        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView title;
        TextView content;
        TextView text_date;
    }
}
